package com.ruijie.acl;

import java.util.HashMap;
import java.util.Map;

import com.ruijie.acl.utils.AclUtil;
import com.ruijie.foundation.bo.EntityObject;
import com.ruijie.foundation.international.web.function.MessageFunctionLibrary;
/**
 * <p>
 * Title: 时间对象
 * </p>
 * <p>
 * Description: 用于数据库中的时间表管理
 * </p>
 * <p>
 * Copyright: Copyright (c) 2008
 * </p>
 * <p>
 * Company: Ruijie Co., Ltd.
 * </p>
 * <p>
 * Create Time: 2009-09-14
 * </p>
 * <p>
 * Update Time:
 * </p>
 * <p>
 * Updater:
 * </p>
 * <p>
 * Update Comments:
 * </p>
 *@author ASAI
 */
public class TimeInfo extends EntityObject {

	/**
	 * serialVersionUID
	 */
	private static final long serialVersionUID = 7822420541954124994L;
	private TimeRange timeRange;
	private String starttime;
	private String endtime;
	private String periodic;
	private String tempPeriodic;// 存放临时的每周有效时间，用于界面显示

	public String getTempPeriodic() {
		/*解析periodic中的星期替换成中文的星期*/
		String[] time = periodic.split(" ");
		String tempPeriodic = "";
		for (int j = 0; j < time.length; j++) {
			tempPeriodic += " "+ MessageFunctionLibrary.i18n_msg_if("acl_enum_time_",time[j]).toString();
		}
		if(tempPeriodic.length()>0){
			this.tempPeriodic = tempPeriodic.substring(1);
		}		
		return this.tempPeriodic;
	}

	public void setTempPeriodic(String tempPeriodic) {
		this.tempPeriodic = tempPeriodic;
	}

	public TimeInfo() {
		periodic = "daily";
	}

	public String getStarttime() {
		return this.starttime;
	}

	public void setStarttime(String starttime) {
		this.starttime = convertTime(starttime);
	}
	
	

	public String getEndtime() {
		return this.endtime;
	}

	public void setEndtime(String endtime) {
		this.endtime = convertTime(endtime);
	}

	public String getPeriodic() {
		/*对星期进行排序*/
		Map<String, Integer> tempMap = new HashMap<String, Integer>();
		tempMap.put("monday", 1);
		tempMap.put("tuesday", 2);
		tempMap.put("wednesday", 3);
		tempMap.put("thursday", 4);
		tempMap.put("friday", 5);
		tempMap.put("saturday", 6);
		tempMap.put("sunday", 7);
		String strTemp = "";
		if (periodic!=null){
			String temp[] = periodic.split(" ");
			for(int i=0;i<temp.length-1;i++){
				for(int j=i+1;j<temp.length;j++){
					if(tempMap.get(temp[i])> tempMap.get(temp[j])){
						strTemp = temp[i];
						temp[i] = temp[j];
						temp[j] = strTemp;
					}
				}
			}
			periodic = "";
			for(int i=0;i<temp.length;i++){
				periodic += " "+temp[i];
			}
			if(periodic.length()>0){
				periodic = periodic.substring(1);
			}
		}
		
		return this.periodic;
	}

	public void setPeriodic(String periodic) {
		this.periodic = periodic;
	}

	public TimeRange getTimeRange() {
		return timeRange;
	}

	public void setTimeRange(TimeRange timeRange) {
		this.timeRange = timeRange;
	}
	@Override
	public boolean equals(Object obj){
		if (obj == this){
			return true;
		}
		if(obj == null || !(obj instanceof TimeInfo)){
			return false;
		}
		TimeInfo timeInfo = (TimeInfo)obj;
		return AclUtil.equalString(this.getPeriodic(), timeInfo.getPeriodic())
			&& AclUtil.equalString(this.getStarttime(), timeInfo.getStarttime())
			&& AclUtil.equalString(this.getEndtime(), timeInfo.getEndtime());
	}
	
	public TimeInfo clone(TimeRange timeRange){
		TimeInfo entity = new TimeInfo();
		entity.setStarttime(this.getStarttime());
		entity.setEndtime(this.getEndtime());
		entity.setPeriodic(this.getPeriodic());
		entity.setTimeRange(timeRange);
		return entity;
	}
	
	/**
	 * 处理时间的位数
	 * @param time
	 * @return
	 */
	protected String convertTime(String time){
		String times[] = time.split(":");
		String hour = times[0];
		String minute = times[1];
		if(times[0].length()==2 && times[0].substring(0, 1).equals("0")){
			hour = times[0].substring(1);
		}
		if(times[1].length()==1){
			minute="0"+times[1];
		}
		return hour+":"+minute;
	}
}
